package com.turing.usdtpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.entity.UserData;
import com.turing.usdtpay.manager.UserManager;

import java.util.ArrayList;

/**
 * @作者：JAY
 * @创建时间：2019/10/24 12:38
 * @描述：
 **/
public class UserAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<UserData> arrayList = new ArrayList<>();
    private Holder holder;
    private OnClickListener listener;

    public UserAdapter(Context context, ArrayList<UserData> arrayList) {
        this.mContext = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user,parent,false);
            holder = new Holder();
            holder.textView = convertView.findViewById(R.id.item_account);
            holder.imageView = convertView.findViewById(R.id.item_delete);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        holder.textView.setText(arrayList.get(position).getAccount());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserManager.getInstance().removeUser(arrayList.get(position).getAccount());
                listener.onClick();
            }
        });
        return convertView;
    }

    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }

    public interface OnClickListener{
        public void onClick();
    }


    public class Holder{
        private TextView textView;
        private ImageView imageView;
    }
}
